﻿@using KPIS.GERP.Models.ViewModel
@using KPIS.GERP.Models.ICM
@using KPIS.GERP.Models.ICM.ViewModel
@model vmPprWithPprItem

@{
    ViewBag.Title = "รวบรวมการแจ้งความประสงค์ขอซื้อ/จ้าง";
    ViewBag.Descr = "รวบรวมข้อมูลการแจ้งความประสงค์ขอซื้อ/จ้าง เพื่อทำการขอซื้อ/จ้างโดยหน่วยงานพัสดุ";
  
}
@using (Html.BeginForm("Create", "GatheringPrePurchaseRequest", FormMethod.Get, new { @class = "form-horizontal" }))
{
    @Html.AntiForgeryToken()

    <p id="message"></p>
    <div class="row-fluid">    
        <div class="block-button-menu-above">
            @*@Html.ButtonSubmitAll() *@
            <button class="btn button-standard-width btn-small btn-success" type="submit"><i class="icon-plus bigger-120"></i>&nbsp;&nbsp;สร้างคำขอซื้อ/จ้างจากรายการที่เลือก</button>
            <a role="button" class="btn button-standard-width btn-small btn-info" href="@Url.Action("Index", "Purchase")">ไปหน้างานขอซื้อขอจ้าง</a>
            <div class="alert alert-info" style="margin-top: 24px">
                <button type="button" class="close" data-dismiss="alert">
                    <i class="icon-remove"></i>
                </button>
                <strong>หมายเหตุ!</strong>

                ข้อมูลที่แสดง คือ รายการพัสดุ/บริการที่ซื้อ/จ้าง ของคำขอแจ้งความประสงค์ที่ผ่านการอนุมัติแล้ว และยังไม่ได้รวบรวม นำไปใช้ในการขอซื้อขอจ้าง
            </div>
        </div>

        @*<div class="row-fluid">
		    <div class="control-group">
			    <div class="control-label">
				    ประเภทการจัดหา
			    </div>
			    <div class="controls">   
				    @Html.DropDownList("PcTypeId", LookupService.MasLookUpSelectList(KPIS.GERP.Models.ICM.IcmMasLookUpType.LookUpType.CommitteeFunction)
                        , new { @class = "chzn-select" })
			    </div>            
		    </div>
            <div class="control-group">
			    <div class="control-label">
				    ปีงบประมาณ
			    </div>
			    <div class="controls">   
				    @Html.DropDownList("BgYear", LookupService.MasLookUpSelectList(KPIS.GERP.Models.ICM.IcmMasLookUpType.LookUpType.CommitteeFunction)
                        , new { @class = "chzn-select" })
			    </div>            
		    </div>
            <div class="control-group">
			    <div class="control-label">
				    แหล่งเงิน
			    </div>
			    <div class="controls">   
				    @Html.DropDownList("BgSourceId", LookupService.MasLookUpSelectList(KPIS.GERP.Models.ICM.IcmMasLookUpType.LookUpType.CommitteeFunction)
                        , new { @class = "chzn-select" })
			    </div>            
		    </div>
        </div>*@
         <div class="alert-error">
        @Html.ValidationSummary()
             </div>
        <div class="row-fluid">
            <div class="table-header span12">
                <div class="span10">
					@ViewBag.Title
                </div>
				@Html.Partial("_ExportButton")
            </div>
        </div>

        @if (!string.IsNullOrWhiteSpace(ViewBag.message_error as string))
        {

            <div class="alert-error">
                @ViewBag.message_error
            </div>
        } 

        <table id="table" class="table table-striped table-bordered table-hover" aria-describedby="table_info">
            <thead>
                <tr>
                    <th class="center" style="width:50px;">
						<!-- Check box delete multiple records column-->
                        <label>
                            <input type="checkbox" />
                            <span class="lbl"></span>
                        </label>
                    </th>
                    <th>
                        @Html.DisplayName("เลขลำดับการแจ้งความประสงค์")
                    </th>
                    <th>
                        @Html.DisplayName("ความสำคัญ")
                    </th>
                    <th>
                        @Html.DisplayName("รายการค่าใช้จ่าย")
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.PrePurchaseRequestItem.FirstOrDefault().Qty)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.PrePurchaseRequestItem.FirstOrDefault().Price)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.PrePurchaseRequestItem.FirstOrDefault().Amount)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.PrePurchaseRequestItem.FirstOrDefault().CreatedWhen)
                    </th>
					<th class="center" style="width:80px;">
						<!-- Empty head column for export buttons    new { data_bind = "value: IsProiority" })-->&nbsp;
					</th>
                </tr>
            </thead>

            <tbody>
                
               @foreach (var p in Model == null || Model.PrePurchaseRequest == null ? new List<PrePurchaseRequest>() : Model.PrePurchaseRequest)
               {
                   foreach (var m in Model.PrePurchaseRequestItem.FindAll(model => model.PprId == p.PprId) ?? new List<PrePurchaseRequestItem>())
                   {
                    <tr>
                        <td class="center">
							<!-- Check box all for delete multiple records -->
                            <label>
                                <input type="checkbox" name="selectIds" value="@(m.PprItemId)" />
                                <span class="lbl"></span>
                            </label>
                        </td>
                        <td>
							 @Html.ActionLink(string.IsNullOrWhiteSpace(p.Code) ? "ไม่ระบุ" : p.Code, "Details", "PrePurchase", new { Id = p.PprId }, new {title=p.Name})
						</td>
                        <td>
							@if (p.Priority.LookUpName == "ปกติ")
           {
                                    @Html.Raw("<font color='green'>")
           }
           else
           {
                                    @Html.Raw("<font color='red'>")
           }
                                @Html.DisplayFor(model => p.Priority.LookUpName)
                                @Html.Raw("</font>")
						</td>
					    
						<td >
						    @*@Html.DisplayFor(model => m.MasBudgetExpense.Name)*@
                            <span title="@(m.ResItemId.HasValue ? m.MasResourceItem.Code : m.MasBudgetExpense.Code)">
                                @(m.ResItemId.HasValue ? m.MasResourceItem.Name : m.MasBudgetExpense.Name)
                            </span>
						</td>
						<td style="text-align: right">
							@Html.DisplayFor(model => m.Qty, m.Qty.HasValue ? m.Qty.Value.ToString("#,##0") : "0")
						</td>
						<td style="text-align: right">
							@Html.DisplayFor(model => m.Price, m.Price.HasValue ? m.Price.Value.ToString("#,##0.00") : "0.00")
						</td>
						<td style="text-align: right">
							@Html.DisplayFor(model => m.Amount, m.Amount.HasValue ? m.Amount.Value.ToString("#,##0.00") : "0.00")
						</td>
						<td>
							@Html.DisplayFor(model => m.CreatedWhen, m.CreatedWhen.HasValue ? m.CreatedWhen.Value.ToString("dd/MM/yyyy") : " ")
						</td>
						
                        <td class="td-actions">
                            <a class="btn btn-small btn-danger button-standard-width rejectbtn1"  href="#" role="button" data-pprid="@p.PprId">ตีกลับ</a>
                            @*@Html.ActionLink("ตีกลับ", "Reject", new { pprid = p.PprId }, new { @class = "btn btn-small btn-danger button-standard-width", role = "button" })*@
                        </td>
                    </tr>
                   }
               }
            </tbody>
        </table>

    </div>
} 
@using (Html.BeginForm("Reject", "GatheringPrePurchaseRequest", new {id = "formreject"}, FormMethod.Post))
{
    <input type="hidden" id="remark" name="remark" /><input type="hidden" id="pprid" name="pprid" /><input id="alternateSubmit" type="submit" />
}
@section scripts {

    
   @* <script src="~/Content/Ace/assets/js/knockout-2.3.0.js"></script>*@
	<script type="text/javascript">

	    $(function () { 
	        var oTable1 = $('#table').dataTable({
	            "aoColumns": [
                    { "bSortable": false },
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    { "bSortable": false }
	            ],
	            "iDisplayLength": -1
	        });

	        $('table th input:checkbox').on('click', function () {
	            var that = this;
	            $(this).closest('table').find('tr > td:first-child input:checkbox')
				.each(function () {
				    this.checked = that.checked;
				    $(this).closest('tr').toggleClass('selected');
				});
	        });

	        $('[data-rel=tooltip]').tooltip();
             



	        $('.rejectbtn1').on("click", function () {
	            var pprid = $(this).data('pprid');
	            var remark = prompt("กรุณากรอกเหตุผลที่ตีกลับ", "");
	            $('#pprid').val(pprid); 
	            $('#remark').val(remark);
	            if (remark != null) {
	                //alert('will reject');
	                $('#alternateSubmit').click();
	            } else {
	                alert('กรุณากรอกเหตุผลที่ตีกลับ');
	            }
	        });
	  
	        
	       

	        //$("#form").submit(function (event) {
	        //    alert("Submitted");

	        //    $.getJSON('/GatheringPrePurchaseRequest/Create', function (result) {
	        //        $('#stage').html('<p> Name: ' + result.message + '</p>');
	        //        $('#stage').append('<p>Age : ' + jd.age + '</p>');
	        //        $('#stage').append('<p> Sex: ' + jd.sex + '</p>');
	        //    });
	        //});

	        //$("#form").submit(function (data) {
	        //    //var currentForm = this;
	        //    //bootbox.confirm(function (result) {
	        //    var result = data;
	        //       // if (result) {
	        //            $.ajax({
	        //                url: "Index",
	        //                type: "get",
	        //                dataType: "json",
	        //                data: $(currentForm).serialize(),
	        //                success: function (result) {
	        //                    if (result.success) {
	        //                        //location.reload();
	        //                    } else {
	        //                        $("#message").html(result.message);
	        //                    }
	        //                }
	        //            });
	                
	        //    //})
	        //   // return false;

	        //});

	        //$("#form").submit(function () {
	        //    var currentForm = this;
	        //    bootbox.confirm(function (result) {
	        //        var _result = result;
	        //        if (result) {
	        //            $.ajax({
	        //                url: "Create",
	        //                type: "post",
	        //                dataType: "json",
	        //                data: $(currentForm).serialize(),
	        //                success: function (result) {
	        //                    if (result.success) {
	        //                        location.reload();
	        //                    } else {
	        //                        $("#message").html(result.message);
	        //                    }
	        //                }
	        //            });
	        //        } else {
	        //            $("#message").html(result.message);
	        //        }
	        //    })
	        //    return false;

	        //});

	    })

	</script>
    }